from app.models.blogs import Blog
from .base import BaseForm
from wtforms import IntegerField, BooleanField, StringField
from wtforms.validators import DataRequired, ValidationError


class LikeForm(BaseForm):
    blogs_id = IntegerField(validators=[DataRequired(message='博客id不能为空')])
    is_like = BooleanField(validators=[DataRequired(message='参数不能为空')])

    def validate_is_like(self, field):
        if field.data not in ['add', 'rm']:
            raise ValidationError('参数错误')

    def validate_blogs_id(self, field):
        if not Blog.query.get(field.data):
            raise ValidationError('博客不存在')


class CommentForm(BaseForm):
    blogs_id = IntegerField(validators=[DataRequired(message='博客id不能为空')])
    content = StringField(validators=[DataRequired(message='评论内容不能为空')])
    parent_id = IntegerField()
